using System.Security.Claims;
using CampusDataOperationLibrary;
using CampusSignInManageSystemWebAPI.Responses.@public;
using CampusSignInManageSystemWebAPI.Responses.Student;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PublicProject.PublicClass.OutPutModels;
using PublicProject.PublicMethods;

namespace CampusSignInManageSystemWebAPI.Controllers;

[ApiController]
[Authorize]
[Route("[controller]/[action]")]
public class StudentController(StudentOperation studentOperation) : ControllerBase
{
    [HttpGet]
    public async Task<OutputModel<List<OutputCourseModel>>> GetCourses()
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var reuslt = await studentOperation.GetCoursesAsync(id);
        var output = reuslt.Select(x => new OutputCourseModel
        {
            CourseName = x.CourseName,
            TeacherName = x.Teacher.Name
        }).ToList();
        return OutPutMethod<List<OutputCourseModel>>.Success(output);
    }

    [HttpGet]
    public async Task<OutputModel<bool>> SignIn([FromQuery] int attendanceId, [FromQuery] int layerId)
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var result = await studentOperation.SignInAsync(id, attendanceId, layerId);
        return OutPutMethod<bool>.Success(result);
    }

    [HttpGet]
    public async Task<OutputModel<List<OutputAttendanceModel>>> GetStudentAttendances([FromQuery] string courseName)
    {
        var id = this.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
        var queryResult = await studentOperation.GetStudentAttendancesAsync(id, courseName);
        var output = queryResult.Select(x => new OutputAttendanceModel
        {
            Date = x.Item1.ToString("yyyy-MM-dd HH:mm:ss"),
            IsPresent = x.Item2,
        }).ToList();
        return OutPutMethod<List<OutputAttendanceModel>>.Success(output);
    }
}